package com.shaoshaohuo.app.ui.ec;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.constant.ListViewAction;
import com.shaoshaohuo.app.entity.BaseEntity;
import com.shaoshaohuo.app.entity.BusinessSeller;
import com.shaoshaohuo.app.entity.BusinessSellerEntity;
import com.shaoshaohuo.app.net.HttpRequest.RequestListener;
import com.shaoshaohuo.app.net.RequestService;
import com.shaoshaohuo.app.ui.BaseActivity;
import com.shaoshaohuo.app.adapter.BusinessRecordAdapter;
import com.shaoshaohuo.app.view.TopbarView;
import com.shaoshaohuo.app.view.XListView;
import com.shaoshaohuo.app.view.XListView.IXListViewListener;
import com.shaoshaohuo.app.view.swipemenu.SwipeMenu;
import com.shaoshaohuo.app.view.swipemenu.SwipeMenuCreator;
import com.shaoshaohuo.app.view.swipemenu.SwipeMenuItem;
import com.shaoshaohuo.app.view.swipemenu.SwipeMenuListView.OnMenuItemClickListener;
import com.shaoshaohuo.app.utils.StringHelper;

import java.util.ArrayList;
import java.util.List;


/**
* @author: yusonglin
* @类   说   明:	
* @version 1.0
* @创建时间：2015年9月14日 下午1:15:04
* 
*/
public class BusinessRecordActivity extends BaseActivity implements IXListViewListener {

    private TopbarView mTopbarView;

    private XListView mListView;
    
    private BusinessRecordAdapter<BusinessSeller> mAdapter;
    
    private List<BusinessSeller> mDataList = new ArrayList<BusinessSeller>();
    private String bigpid = "";
    private String catid = "";
    private String pid = "";
    private String cityid = "";
    private String action = ListViewAction.NEW2;
    private String cursor = "";
    private int offset = 10;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_business_record);
        initView();
        setUpView();
        requestData(true);
    }

    private void initView() {
        mTopbarView = (TopbarView) this.findViewById(R.id.topbar);

        mListView = (XListView) this.findViewById(R.id.listview_data_list);
    }

    private void setUpView() {
        mTopbarView.setCenterText("历史商机");
        mTopbarView.setLeftView(true, true);
        setListView();
    }

    private void requestData(boolean isLoading) {
        if(isLoading){
            startLoadingDialog();
        }
        RequestService.getInstance().getMyBusinessList(this,  bigpid, pid, catid, cityid, cursor, action, offset+"", BusinessSellerEntity.class, new RequestListener() {
            
            @Override
            public void onSuccess(int requestCode, BaseEntity resultData) {
                dismissLoadingDialog();
                if(resultData.isOk()){
                    BusinessSellerEntity entity = (BusinessSellerEntity) resultData;
                    List<BusinessSeller> list = entity.getData().getList();
                    mDataList.addAll(list);
                    
                    if ((mDataList.size() < offset) || (action.equals(ListViewAction.HISTORY2) && list.size() < offset)) {
//                      mListView.setPullLoadEnable(false);
                        mListView.endLoadingMore("没有更多了");
                    }
                    setAdapter(mDataList);
                    
                }else{
                    showToast(resultData.getMsg());
                }
                onLoad();
            }
            
            @Override
            public void onFailed(int requestCode, Exception error, String msg) {
                dismissLoadingDialog();
                showToast(msg);
                onLoad();
            }
        });
    }

    protected void setAdapter(List<BusinessSeller> mDataList2) {
        
        if(mAdapter == null){
            mAdapter = new BusinessRecordAdapter<BusinessSeller>(this, mDataList2, false);
            mListView.setAdapter(mAdapter);
        }else{
            mAdapter.setData(mDataList2);
        }
    }

    private void setListView() {
        mListView.setPullLoadEnable(true);
        mListView.setPullRefreshEnable(true);
        mListView.setXListViewListener(this);
        
        mListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                PurchaseRecord order = (PurchaseRecord) parent.getItemAtPosition(position);
//                Intent intent = new Intent(BusinessRecordActivity.this,PurchaseRecordDetailActivity.class);
//                intent.putExtra(ExtraName.id, order.getId());
//                startActivity(intent);
//              Intent intent = new Intent(PurchaseRecordActivity.this, PurchaseRecordDetailActivity.class);
//              startActivity(intent);
            }
        });
        
        SwipeMenuCreator creator = new SwipeMenuCreator() {

            @Override
            public void create(SwipeMenu menu) {
                // create "delete" item
                SwipeMenuItem deleteItem = new SwipeMenuItem(
                        getApplicationContext());
                // set item background
                deleteItem.setBackground(new ColorDrawable(Color.parseColor("#FF6368")));
                // set item width
                deleteItem.setWidth(StringHelper.dip2px(BusinessRecordActivity.this,50));
                // set a icon
                deleteItem.setIcon(R.drawable.icon_delete);
                
                // add to menu
                menu.addMenuItem(deleteItem);
            }
        };
        // set creator
        mListView.setMenuCreator(creator);
        
        mListView.setOnMenuItemClickListener(new OnMenuItemClickListener() {
            
            @Override
            public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
                BusinessSeller address = mDataList.get(position);
                showDialog("確定刪除 此条商机?", address);
                return false;
            }
        });
        
    }
    
    private void showDialog(String message, final BusinessSeller id) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(message);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                deleteBusiness(id);
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        builder.create().show();
    }
    
    protected void deleteBusiness(final BusinessSeller id) {
        startLoadingDialog();
        RequestService.getInstance().deleteBusiness(this, id.getId(), BaseEntity.class, new RequestListener() {
            
            @Override
            public void onSuccess(int requestCode, BaseEntity resultData) {
                dismissLoadingDialog();
                if(resultData.isOk()){
                    showToast("删除成功");
                    mDataList.remove(id);
                    mAdapter.setData(mDataList);
                }else{
                    showToast(resultData.getMsg());
                }
            }
            
            @Override
            public void onFailed(int requestCode, Exception error, String msg) {
                dismissLoadingDialog();
                showToast(R.string.please_check_network);
            }
        });
    }

    @Override
    public void onRefresh() {
        if(mDataList.size()>0){
            cursor = mDataList.get(0).getCursor();
        }else {
            cursor = "";
        }
        action = ListViewAction.NEW2;
        mListView.setPullLoadEnable(true);
        requestData(false);
    }

    @Override
    public void onLoadMore() {
        if(mDataList.size()>0){
            cursor = mDataList.get(mDataList.size()-1).getCursor();
        }else {
            cursor = "";
        }
        action = ListViewAction.HISTORY2;
        requestData(false);
    }

    private void onLoad() {
        mListView.stopRefresh();
        mListView.stopLoadMore();
    }
    
}
